package com.bailian.app.controller.Apis;

import com.bailian.common.annotation.Anonymous;
import com.bailian.common.core.controller.BaseController;
import com.bailian.common.core.domain.AjaxResult;
import com.bailian.common.core.page.TableDataInfo;
import com.bailian.system.domain.BlNews;
import com.bailian.system.domain.BlNewsType;
import com.bailian.system.service.IBlNewsService;
import com.bailian.system.service.IBlNewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资讯控制器
 * 
 * @author bailian
 */
@Api(tags = "资讯相关接口")
@RestController
@RequestMapping("/app/api/news")
public class NewsController extends BaseController {

    @Autowired
    private IBlNewsService newsService;
    
    @Autowired
    private IBlNewsTypeService newsTypeService;

    /**
     * 获取咨询列表
     */
    @Anonymous
    @ApiOperation("获取咨询列表")
    @GetMapping("/list")
    public TableDataInfo getNewsList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("类型ID") @RequestParam(required = false) String typeId) {
        
        startPage();
        BlNews news = new BlNews();
        if (typeId != null && !typeId.isEmpty()) {
            news.setTypeId(typeId);
        }
        
        List<BlNews> list = newsService.selectBlNewsList(news);
        return getDataTable(list);
    }

    /**
     * 获取资讯分类
     */
    @Anonymous
    @ApiOperation("资讯分类")
    @GetMapping("/category")
    public TableDataInfo getNewsCategory() {
        BlNewsType param = new BlNewsType();
        List<BlNewsType> list = newsTypeService.selectBlNewsTypeList(param);
        return getDataTable(list);
    }
    
    /**
     * 获取咨询详情
     */
    @Anonymous
    @ApiOperation("获取咨询详情")
    @GetMapping("/{id}")
    public AjaxResult getNewsDetail(@ApiParam("咨询ID") @PathVariable Long id) {
        try {
            BlNews news = newsService.selectBlNewsById(id);
            if (news == null) {
                return error("咨询不存在");
            }
            return success(news);
        } catch (Exception e) {
            return error("获取咨询详情失败：" + e.getMessage());
        }
    }
} 